<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Index extends MY_Controller {
    
    var $controller_path 	    = '/dict/index/';
    var $model_path 		    = 'dict/dict_model';
    var $lang_path 		    = 'dict/dict';
	
    function __construct()
    {
		parent::__construct();
		log_message('debug', "dict/index Index controller Class Initialized.");
    }
	
    public function index()
    {
        $map_arr = NULL;
        $seach_arr = $this->get_input("columnFilters"); //获得数据
        //print_r($seach_arr);
        $map_arr["label"] = $seach_arr->label->value;

        $total_nums = $this->dict_model->lists(NULL,$map_arr);
        $ret_arr = $this->get_page($total_nums,$this->get_input("pageNum"),$this->get_input("pageSize"));
        $ret_arr["data"]["content"] = $this->dict_model->lists($ret_arr["limit"],$map_arr);
        if($ret_arr["data"]["content"])
        {
        	foreach($ret_arr["data"]["content"] as &$var)
        	{
        		//
        	}
        }
		$this->api("200",$ret_arr["data"],"");
    }

    public function delete()
    {
        $ret_data['type'] = 304;
        $ret_data['message'] = '';
        $ret_data['data'] = NULL;

        $ids = $this->get_input();
        $ids_arr = NULL;
        $type = false;
        if($ids)
        {
            foreach($ids as $val)
            {
                $ids_arr[] = $val->id;
            }
            $type = $this->dict_model->dels($ids_arr);
        }
        if($type) {
            $ret_data['type'] = 200;
            $ret_data['message'] = $this->lang->line('dict_success');
        }
        else {
            $ret_data['message'] = $this->lang->line('dict_error');
        }
        $this->write_logs($ret_data['type'],$ret_data['data'],"字典删除：".$ret_data['message']);
        $this->api($ret_data['type'],$ret_data['data'],$ret_data['message']);
    }

    public function save()
    {
        $ret_data['type'] = 304;
        $ret_data['message'] = '';
        $ret_data['data'] = NULL;

        $post_data = $this->get_input("","array");
        $this->form_validation->set_data($post_data);
        $this->form_validation->dir_set_rules('menu/menu');
        if($this->form_validation->run() == true) {

            if($post_data["id"]) $data_arr['id'] = $post_data["id"];

            $data_arr['label'] = $post_data["label"];
            $data_arr['value'] = $post_data["value"];
            $data_arr['type'] = $post_data["type"];
            $data_arr['sort'] = $post_data["sort"];
            $data_arr['description'] = $post_data["description"];
            $data_arr['remarks'] = $post_data["remarks"];

            $type = $this->dict_model->saves($data_arr);
            if($type) {
                $ret_data['type'] = 200;
                $ret_data['data'] = $type;
                $ret_data['message'] = $this->lang->line('dict_success');
            }
            else {
                $ret_data['message'] = $this->lang->line('dict_error');
            }
        }
        else {
            $ret_data['message'] = $this->form_validation->error_string();
        }
        $this->write_logs($ret_data['type'],$ret_data['data'],"字典更新：".$ret_data['message']);
        $this->api($ret_data['type'],$ret_data['data'],$ret_data['message']);
    }
}
